package com.ymttest.database.sqlwapper;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ymt.core.tool.Logger;
import com.ymt.database.SqlManager;
import com.ymttest.database.model.YmtLive;
import com.ymttest.utils.MapUtil;
import com.ymttest.utils.YMTDateUtil;

public class YmtLiveWapper {
	SqlManager manage;

	private String mapperClzName = "com.ymttest.database.mapping.YmtLiveMapper.";
	
	public YmtLiveWapper() {
		manage = new SqlManager("appsit1");
	}
	
	
	/**
	 * 关闭数据库连接
	 */
	public void close() {
		manage.close();
	}

	/**
	 * 返回sqlmanager
	 * 
	 * @return
	 */
	public SqlManager getManager() {
		return manage;
	}

	
	@Override
	protected void finalize() throws Throwable {
		this.close();
		manage = null;
		super.finalize();
	}
	
	
	
	public YmtLive selectByLiveId(Integer liveId) throws InterruptedException{
		return (YmtLive) manage.getSelectResults(mapperClzName + "selectByLiveId", liveId).get(0);
	}
	
	public List<YmtLive> selectEffectiveLive(){
		Map<String, Object> map = new HashMap<>();
		map.put("iAction", 1);
		map.put("dEndTime", YMTDateUtil.getDate());
		return (List<YmtLive>) manage.getSelectResults(mapperClzName + "selectEffectiveLive",map);
	}
	
	
	public int updateTimeBySellerId(Map<String, Object> record) throws InterruptedException{
		int i = manage.update(mapperClzName + "updateTimeBySellerId", record);
		manage.commit();
		// manage.close();
		return i;
	}
	
	
	public int updateProductCountByLiveId(int liveId,int productCount) throws InterruptedException{
		int i = manage.update(mapperClzName + "updateProductCountByLiveId", MapUtil.hashMap("liveId",liveId,"productCount",productCount));
		manage.commit();
		// manage.close();
		return i;
	}
	
	
	public int updateComfirmByLiveId(int liveId) throws InterruptedException{
		int i = manage.update(mapperClzName + "updateComfirmByLiveId", MapUtil.hashMap("iActivityId",liveId));
		manage.commit();
		// manage.close();
		return i;
	}
	
	
	public List<YmtLive> selectBySellerIdAndInDuringTime(Integer sellerId,Date startDate,Date endDate){
		Map<String, Object> map = new HashMap<>();
		map.put("iSellerId", sellerId);
		map.put("dBeginTime", startDate);
		map.put("dEndTime", endDate);
		
		return (List<YmtLive>) manage.getSelectResults(mapperClzName + "selectBySellerIdAndInDuringTime",map);
	}
	
	
	
	public int updateTimeBySellerIdAndLiveId(Map<String, Object> record) throws InterruptedException{
		int i = manage.update(mapperClzName + "updateTimeBySellerIdAndLiveId", record);
		manage.commit();
		// manage.close();
		return i;
	}
	
	public List<Map> selectforMapByLiveId(Integer liveId) throws InterruptedException {
	    @SuppressWarnings("unchecked")
	    List<Map>  livelist = manage.getSelectResults("com.ymttest.database.mapping.YmtLiveMapper.selectforMapByLiveId", liveId);
//	    manage.commit();
	    return livelist;
	}
	
	
	@SuppressWarnings("unchecked")
	public List<Map<String, Object>> selectforMapBySellerId(Integer sellerId){
		return (List<Map<String, Object>>)manage.getSelectResults("com.ymttest.database.mapping.YmtLiveMapper.selectforMapBySellerId", sellerId);
	}
	
	
	
	
	
	public static void main(String[] args) throws InterruptedException {
		
	//System.out.println(new YmtLiveWapper().selectByLiveId(148114));
	System.out.println(new YmtLiveWapper().updateComfirmByLiveId(160416));
	}
}
